package cn.yunhe.services;

import cn.yunhe.dao.MerchantsDataDao;
import cn.yunhe.entity.merchantsentity.MerchantsData;

/*
 * 郭慧广
 *
 * 商家分页
 *
 * 李非凡更改
 * */

import cn.yunhe.util.FenYe2;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MerchantsFenYeService {
    SqlSession sqlSession = null;
    MerchantsDataDao merchantsDataDao = null;

    public Map getMerchantsDataPage(String pageIndex) {
        sqlSession = SqlSessionFactoryUtil.getSqlSession();
        merchantsDataDao = sqlSession.getMapper(MerchantsDataDao.class);

        //判断当前页是否为null
        if (pageIndex == null || pageIndex == "") {
            pageIndex = "1";
        }
        int pageNum = Integer.parseInt(pageIndex);
        //总条数
        int count = merchantsDataDao.getAllCount();
        int total = FenYe2.getTotalPages(count, FenYe2.pageSize); //总页数
        //页数小于1 显示第一页
        if (pageNum < 1) {
            pageNum = 1;
        }
        //页数大于总页数 显示最后一页
        if (pageNum > total) {
            pageNum = total;
        }

        //计算从第几条显示 qiShi 和显示几条数据 jieShu
        Map map = new HashMap();
        int qiShi = (pageNum - 1) * FenYe2.pageSize;
        int jieShu = FenYe2.pageSize;


        map.put("qiShi", qiShi);
        map.put("jieShu", jieShu);

        List<MerchantsData> merchantsDataList = merchantsDataDao.getMerchantsDataPage(map);
        Map map2=new HashMap();
        map2.put("merchantsDataList",merchantsDataList);
        map2.put("pageNum",pageNum);
        map2.put("total",total);
        return map2;
    }


}
